<?php
/**
 * Copyright (c) 2010, Slawomir Mazur <mazursllawek@gmail.com>
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * o Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the Softisland nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Enter description here...
 *
 * @package     default
 * @copyright   Slawomir Mazur (c) 2010 (http://www.zend.com/zce.php?c=ZEND011643)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @author      Slawomir Mazur <mazursllawek@gmail.com>
 */

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initDb()
	{
		$db = null;
		if ($this->hasPluginResource('db')) {
			$resource = $this->getPluginResource('db');
			$resource->init();
			$db = $resource->getDbAdapter();
			$db->query('SET NAMES utf8');
			Zend_Registry::set('db', $db);
		}
		return $db;
	}
	
	protected function _initSession()
	{
		$this->bootstrap('db');
		if ($this->hasPluginResource('session')) {
			$resource = $this->getPluginResource('session');
			$resource->init();
		}
	}
	
	protected function _initLog()
	{
		// Create the Zend_Log object
		$logger = new Zend_Log();

		$streamWritter = new Zend_Log_Writer_Stream(BASE_PATH . '/public/var/log/gp.log');
		$streamWritter->addFilter(Zend_Log::DEBUG);
		$streamWritter->addFilter(Zend_Log::INFO);
		$logger->addWriter($streamWritter);

		// Create the Firebug writter - development only
		if ($this->_application->getEnvironment() != 'production') {
			// Init firebug logging
			$firebugWriter = new Zend_Log_Writer_Firebug();
			$firebugWriter->setDefaultPriorityStyle('TRACE');
			$logger->addWriter($firebugWriter);
		}
		Zend_Registry::set('log', $logger);
		
		return $logger;
	}
	
	protected function _initCache() 
	{
		$this->bootstrap('session');
		
		$_COOKIE = array('user' => 
				Gp_Auth_UserAuth::getInstance()->getUser()? 1 : 2);		
		
		// extra page caching for IndexController actions
		//Gp_Cache_Manager::getPageCacheObject()->start();
		
		// standard file caching
		$cache = null;
		if ($this->hasPluginResource('cachemanager')) {			
			$cache = $this->getPluginResource('cachemanager')
						 ->getCacheManager()
						 ->getCache('file');
		}
		Zend_Registry::set(Gp_Cache_Manager::FILE_CACHE, $cache);

		return $cache;
	}
	
	protected function _initRoutes()
	{
		$this->bootstrap('frontcontroller');
		$this->bootstrap('db');
		
		$frontController = $this->getResource('frontcontroller');
		/* @var $router Zend_Controller_Router_Rewrite */
		$router = $frontController->getRouter();
		
		$admin = 'zmienna_nazwa/?';
		
		$router->addRoute('admin',
	    			new Zend_Controller_Router_Route_Regex(
	    				$admin,
	    				array(
	    					'module' => 'admin',
	    					'controller' => 'index',
	    					'action' => 'index'
	    				)
	    			)
	    		);
	    		
		return $router;
	}
	
	protected function _initView()
	{
		// Initialize view
		$view = new Zend_View();

		// Add it to the ViewRenderer
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);

		// Return it, so that it can be stored by the bootstrap
		return $view;
	}
	
	protected function _initActionHelpers()
	{
		Zend_Controller_Action_HelperBroker::addPrefix('Gp_Controller_Action_Helper');
	}
	
	protected function _initViewHelpers()
	{
		$this->bootstrap(array('frontcontroller', 'view'));
		$frontController = $this->getResource('frontcontroller');
		$view = $this->getResource('view');
		// closing tags
		$view->doctype()->setDoctype(Zend_View_Helper_Doctype::XHTML1_STRICT);
		
		// Add helper paths.
		$view->addHelperPath('Gp/View/Helper', 'Gp_View_Helper');
		
		// ... and some script paths mainly for global partials
		$view->addScriptPath(APPLICATION_PATH . '/modules/cms/views/scripts');
		
		// Add Stylesheet's
		$view->headLink()->appendStylesheet($view->baseUrl('skin/css/main.css'));
		
		// Add JavaScript's
		$view->headScript()->appendFile($view->baseUrl('js/jq.js'));
		$view->headScript()->appendFile($view->baseUrl('js/jq.validate.js'));
	}
}